﻿#coding=utf-8
from sqlobject import *
import sqlobject
from sqlobject.sqlite import builder

import datetime
import time
from Queue import Queue as queue
from threading import Thread
import traceback


__doc__='''  DataModel for database storage  '''


SqliteConn = sqlobject.sqlite.builder() #@UndefinedVariable
sqlhub.processConnection = SqliteConn('h:/harvester/data.s3db')
#sqlhub.processConnection = SqliteConn('/harvester/z/datas.s3db')


# print dbfilename,' connected.'

# tables

class Connectable(SQLObject):
    _connection = sqlhub.processConnection
    # 是否输出sql语句
    _connection.debug = False
    # 是否延迟更新 就是在执行xxx.xx = xxx的同时更新数据库对应字段的内容
    class sqlmeta:
        lazyUpdate = True

# 站点表
class Site(Connectable):
    name = UnicodeCol(length=100,default='')
    needproxy = StringCol(length=10,default='False')
    forums = MultipleJoin('Forum')
#print u'正在建立数据库表,如果她们不存在的话..'
Site.createTable(ifNotExists=True)
# 论坛版面表
class Forum(Connectable):
    # 默认初始化一个site
    def getFirstSite(): #@NoSelf
        Site.createTable(ifNotExists=True)
        q = Site.select(limit=1)
        if len(list(q)) == 0:
            return Site(name='tempSite')
        return q[0]
    site = ForeignKey('Site',default=getFirstSite().id)
    title = UnicodeCol(length=100,default='')
    name = UnicodeCol(length=200,default='')
    forumUrl = UnicodeCol(length=100,default='')
    text = UnicodeCol(default='')
    postCount = IntCol(default=0)
    lastUpdate = DateTimeCol(default=datetime.datetime.now)
    posts = MultipleJoin('Post')

# 帖子表
class Post(Connectable):
    title = UnicodeCol(length=300,default='')
    titleSimple = UnicodeCol(length=300,default='')
    pageUrl = UnicodeCol(length=200,default='')
    forum = ForeignKey('Forum')
    raw = SingleJoin('RawContent')
    urls = MultipleJoin('Url')
    content = MultipleJoin('PostContent')
    author = UnicodeCol(length=50,default='')
    images = MultipleJoin('Image')
    torrents = MultipleJoin('Torrent')
    postDate = UnicodeCol(length=50,default='')
    postDateTime = DateTimeCol(default=datetime.datetime.now)
    urlHash = IntCol(default=0)
    state = UnicodeCol(length=20,default='')
    
# 主题主要内容表
class PostContent(Connectable):
    content = UnicodeCol(default='')
    post = ForeignKey('Post')
    def __eq__(self,other):
        if other == None:return False
        return self.content== other.content and True or False

# 外部连接表, 一般保存种子地址
class Url(Connectable):
    name = UnicodeCol(default='')
    post = ForeignKey('Post')
    torrents = MultipleJoin('Torrent')
    def __eq__(self,other):
        if other == None:return False
        return self.name == other.name and True or False

# 图片表
class Image(Connectable):
    filename = UnicodeCol(default='')
    downloaded = IntCol(default=0)
    fileLength = IntCol(default=0)
    url = UnicodeCol(default='')
    post = ForeignKey('Post')

# 帖子原始内容表
class RawContent(Connectable):
    post = ForeignKey('Post')
    content = UnicodeCol(default='')

class Torrent(Connectable):
    filename = UnicodeCol(default='')
    download_time = DateTimeCol(default=datetime.datetime.now)
    download = IntCol(default=0)
    url = ForeignKey('Url')
    fileLength = IntCol(default=0)
    post = ForeignKey('Post')

class Word(Connectable):
    name = UnicodeCol(default='')
    freq = IntCol(default=0)

class WordRela(Connectable):
    word = ForeignKey('Word')
    post = ForeignKey('Post')

class VisitLog(Connectable):
    visit_time = DateTimeCol(default=datetime.datetime.now)
    type = IntCol(default=0)
    user_id = IntCol(default=0)
    obj = UnicodeCol(default='')
    ip_addr = UnicodeCol(default='')
    
class SysLog(Connectable):
    type = IntCol(default=0)
    msg = UnicodeCol(default='')
    visit_time = DateTimeCol(default=datetime.datetime.now)

# create tabes if neccessary

Forum.createTable(ifNotExists=True)
Post.createTable(ifNotExists=True)
Url.createTable(ifNotExists=True)
PostContent.createTable(ifNotExists=True)
RawContent.createTable(ifNotExists=True)
Image.createTable(ifNotExists=True)
Torrent.createTable(ifNotExists=True)
Word.createTable(ifNotExists=True)
WordRela.createTable(ifNotExists=True)
VisitLog.createTable(ifNotExists=True)
SysLog.createTable(ifNotExists=True)


